package Work;

import java.util.ArrayList;
import java.util.Scanner;

/******图书查询、修改、创建、存储*****/

public class ChangeBook {
    //修改
    public void change(ArrayList<Book> list, Jdbc db){
        int c;
        String str;
        String b_id;//记录修改信息
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入待修改书籍的“编号”：");
        b_id = sc.next();
        System.out.println("请选择要修改书籍的：\n1.名称\n2.作者\n3.价格\n4.数量\n（取消操作请按-1）");
        c = sc.nextInt();
        if(c == 1) {
        	System.out.println("请输入待修改书籍新的“名称”：");
            str = sc.next();
            db.sql("update book set b_name = '" + str + "' where b_id = '" + Integer.parseInt(b_id) + "'");
        }else if(c == 2) {
        	System.out.println("请输入待修改书籍新的“作者”：");
            str = sc.next();
            db.sql("update book set author = '" + str + "' where b_id = '" + Integer.parseInt(b_id) + "'");
        }else if(c == 3) {
        	System.out.println("请输入待修改书籍新的“价格”：");
            str = sc.next();
            db.sql("update book set price = '" + str + "' where b_id = '" + Integer.parseInt(b_id) + "'");
        }else if(c == 4) {
        	System.out.println("请输入待修改书籍新的“数量”：");
            str = sc.next();
            db.sql("update book set amount = '" + str + "' where b_id = '" + Integer.parseInt(b_id) + "'");
        }
        /*
        System.out.println("确认修改 ‘" + b_id + "’这本书吗？(键入1确认)");
        c = sc.nextInt();
        if(c==1){
        	//System.out.println(c);
            for (Book book : list) {
                if (book.getId().equals(b_id)) {
                    System.out.println(book.toString());
                    System.out.println("请输入图书新的name(输入-1，不修改):");
                    str = sc.next();
                    //记录修改信息
                    String s = "";
                    if (!str.equals("-1")) {
                        s += "b_name: ‘" + book.getName() + "’ --> ‘";
                        book.setName(str);
                        s += book.getName() + "’; ";
                        db.sql("update book set b_name = '" + str + "' where b_id = '" + Integer.parseInt(b_id) + "'");
                    }
                    System.out.println("请输入图书新的author:");
                    str = sc.next();
                    if (!str.equals("-1")) {
                        s += "author: ‘" + book.getAuthor() + "’ --> ‘";
                        book.setAuthor(str);
                        s += book.getAuthor() + "’; ";
                        db.sql("update book set author = '" + str + "' where b_id = '" + Integer.parseInt(b_id) + "'");
                    }
                    System.out.println("请输入图书新的price:");
                    d = sc.nextDouble();
                    if (!(d + "").equals("-1") && d >= 0) {
                        s += "price: ‘" + book.getPrice() + "’ --> ‘";
                        book.setPrice(d);
                        s += book.getPrice() + "’; ";
                        db.sql("update book set price = '" + d + "' where b_id = '" + Integer.parseInt(b_id) + "'");
                    }
                    System.out.println("请输入图书新的amount:");
                    c = sc.nextInt();
                    if (!(c + "").equals("-1") && c >= 0) {
                        s += "amount: ‘" + book.getAmount() + "’ --> ‘";
                        book.setAmount(c);
                        s += book.getAmount() + "’; ";
                        db.sql("update book set amount = '" + c + "' where b_id = '" + Integer.parseInt(b_id) + "'");
                    }
                    System.out.println("修改完毕(id不可以修改)!");
                    db.record("修改图书信息", b_id, s);
                    System.out.println(book.toString());
                    return;
                }
            }
        }*/else {
            System.out.println("已经取消修改！");
        }
 
    }
 
    //打印列表
    public void printList(ArrayList<Book> list){
        System.out.println("\n\n《图书列表》\n");
        System.out.println("ID\t\t\tName\t\t\t\tAuthor\t\t\t\tPrice\t\tAmount");
        //遍历book列表，并打印对应属性
        for(Book book:list){
            System.out.println(book.getId()+"\t\t"+book.getName()+"\t\t\t\t"
                    +book.getAuthor()+"\t\t\t\t"+book.getPrice()+"\t\t"+book.getAmount());
        }
    }
 
    //通过书号和书名查询书籍
    public void search(ArrayList<Book> list, Jdbc db){
        Scanner sc = new Scanner(System.in);
        PrintList printlist = new PrintList();
        //String str;
        //,demoStr;//demoStr用于记录要操作的图书编号
        int i;
        while(true){
            System.out.println("\n请输入您想要查询的书籍信息表中哪种信息？\n1.书籍编号\n2.书名\n3.作者名\n(输入“-1”退出查询系统):");
            i = sc.nextInt();
            if(i == -1){//用户退出
                return;
            }
            else{
            	ArrayList<Book> book = db.findBook(i);
            	if(book == null) {
            		System.out.println("您输入的信息不存在,请重新输入。");
            	}
            	else{
            		//System.out.println(book.toString());
            		printlist.printbookList(book);
            	}
            	/*
                //不适用 for-each 因为可能出现删除操作
                for(int i=0;i<list.size();i++) {
                    //查找到对应的id或者name
                    if (str.equals(list.get(i).getId()) || str.equals(list.get(i).getName())) {
                        System.out.println(list.get(i).getId() + "\t\t" + list.get(i).getName() + "\t\t"
                                + list.get(i).getAuthor() + "\t\t" + list.get(i).getPrice() + "\t\t" + list.get(i).getAmount());
                        demoStr = list.get(i).getId();
                        flag = true;
                        int c;
                        System.out.println("修改图书属性(1)--删除图书(2)--取消操作(-1)");
                        c = sc.nextInt();
                        if (c == 1) {
                            change(list, demoStr, db);
                            break;
                        } else if (c == 2) {
                            delete(list, demoStr, db);
                            break;
                        } else {
                            break;//输入其他字符后跳回查询
                        }
                    }
                }*/
                //if(!flag){
                    //System.out.println("您输入的图书name或id ‘" + str + "’ 不存在,请重新输入。");
                //}
            }
        }
 
    }
    
    //增加书籍
    public void insert(ArrayList<Book> list, Jdbc db){
        String str1, str2, s="";
        double d;
        int c;
        Scanner sc = new Scanner(System.in);
        while(true){
            boolean flag = true;//判断图书名称是否合法
            Book book = new Book();
            System.out.println("请输入您要添加的图书的name:");
            str1 = sc.next();
            for(Book bookDemo:list){//图书名称不得重复（遍历书籍表）
                if(bookDemo.getName().equals(str1)){
                    System.out.println("已经存在此图书，请勿添加重复图书。\n" +
                            "若是在那个要添加同名书籍，请在书名后方加上大写字母区分\n");
                    flag = false;
                    break;
                }
            }
            if(flag){//书名输入正确才能添加
                book.setName(str1);
                System.out.println("请输入您要添加的图书的author:");
                str2 = sc.next();
                book.setAuthor(str2);
                System.out.println("请输入您要添加的图书的price:");
                d = sc.nextDouble();
                if(d >= 0){
                    book.setPrice(d);
                }
                System.out.println("请输入您要添加的图书的amount:");
                c = sc.nextInt();
                book.setAmount(c);
                System.out.println(book.toString());
                list.add(book);
                db.sql("insert into book (b_name, author, price, amount) " +
                        "value ('" + str1 + "', '" + str2 + "', '" + book.getPrice() + "', '" + c + "')");
                db.record("创建图书信息", str1, "创建成功");//写入记录表格，可删
 
            }
            System.out.println("输入-1退出增加图书(输入其他内容则继续添加):");
            str1 = sc.next();
            if(str1.equals("-1")){
                return;
            }
        }
 
    }
 
    //删除
    public void delete(ArrayList<Book> list, Jdbc db){
        int c;
        //String str;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的书籍编号（若取消操作请输入-1）：");
        //str = sc.next();
        c = sc.nextInt();
        //System.out.println(str);
        if(c == -1) {
        	System.out.println("已经取消删除！");
        }else {
        	db.sql("delete from book where b_id = " +c);
        	System.out.println("编号为“"+c+"”的书籍删除成功!");
        	/*for(int i=0; i<list.size();i++) {
        		//System.out.println(list.size());
                if (list.get(i).getId().equals(str)) {
                    Book demo = list.remove(i);
                    System.out.println(i);
                    System.out.println("图书 ‘" + demo.getName() + "’ 已经删除!");
                    db.sql("delete from book where b_id = " + Integer.parseInt(demo.getId()));
                    db.record("删除图书信息", demo.getId(), "删除成功");
                    printList(list);
                    return;
                }
            }*/
        }
    }
 
    //读取数据库文件
    public ArrayList<Book> read(Jdbc db){
        ArrayList<Book> list = null;
        //初始化Book类的count和idCount
        Book.setCount();
        try{
            list=db.searchBook();
 
            System.out.println("读取数据成功");
            return list;
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }
 
    public void printMessage(Jdbc db){
        ArrayList<Message> list = db.searchMessage();
        System.out.println("\n\n《记录列表》\n");
        System.out.println("ID\t\t\t\tBook\t\t\t\tType\t\t\t\tTime\t\tInfo");
        //遍历book列表，并打印对应属性
        for(Message message:list){
            System.out.println(message.getId()+"\t\t"+message.getBook()+"\t\t\t\t"
                    +message.getType()+"\t\t\t\t"+message.getTime()+"\t\t"+message.getInfo());
        }
    }

}
